package com.atguigu.guli.service.oss.controller;



import com.atguigu.guli.service.base.model.Testpojo;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.oss.service.FileService;

import com.mysql.cj.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

@Api(tags = "阿里云文件管理")
 //跨域
@RestController
@RequestMapping("/admin/oss/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     * @param file
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file") MultipartFile file,

            @ApiParam(value = "模块", required = true)
            @RequestParam("module") String module) throws IOException {

        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String uploadUrl = fileService.upload(inputStream, module, originalFilename);

        //返回r对象
        return R.ok().message("文件上传成功").data("path", uploadUrl);
    }

    @ApiOperation("文件删除")
    @DeleteMapping("/delete")
    public R delete(@RequestParam(value = "path")
                    @ApiParam(value = "文件url",required = true) String path,
                    @RequestParam(value = "module")
                    @ApiParam(value = "桶中文件地址",required = true) String module){

        fileService.delete(path,module);
        return R.ok().message("文件删除成功");
    }

    @ApiOperation("测试方法")
    @GetMapping("/test")
    public String test1() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("success");
        return "调用成功";
    }
//
//    @ApiOperation("第二种测试")
//    @GetMapping("/test2")
//    public void test2(String username,String password){
//        System.out.println(username+password);
//    }
//
//    @ApiOperation("第三种测试")
//    @PostMapping("/test3")
//    public void test3(@RequestBody Testpojo testpojo){
//        System.out.println(testpojo);
//    }
}